<template>
  <div class="app-container home">
    <div class="mb-[10px]">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item label="产品名称" prop="name">
          <el-input v-model="queryParams.name" placeholder="请输入产品名称" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="产品类别" prop="type">
          <el-select v-model="queryParams.type" placeholder="产品类别" clearable>
            <el-option v-for="dict in options" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-row :gutter="10">
        <el-col :span="1.5">
          <el-button type="primary" plain @click="handleAdd">选择数字人</el-button>
        </el-col>
      </el-row>
    </div>

    <el-card shadow="hover">
      <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" />
        <el-table-column label="产品名称" align="center" prop="productName" />
        <el-table-column label="产品话术" align="center" prop="script" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <!-- <el-button link type="primary" @click="handleDelete1111(scope.row)">详情</el-button> -->
            <el-button link type="primary" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" :total="total" @pagination="getList" />
    </el-card>

    <el-dialog v-model="dialogVisible" :title="dialog.title" width="1200px" append-to-body>
      <el-table v-loading="loading" :data="dataListarr" @selection-change="handleSelectionChangearr">
        <el-table-column type="selection" width="55" />
        <el-table-column label="名称" align="center" prop="name" />
        <el-table-column label="视频url" align="center" prop="videoUrl">
          <template #default="scope">
            <video :src="scope.row.videoUrl" class="video-js" :playsinline="true"></video>
          </template>
        </el-table-column>
        <el-table-column label="音频url" align="center" prop="audioUrl" />
      </el-table>
      <pagination v-show="total > 0" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" :total="total" @pagination="getList" />

      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" :loading="buttonLoading" @click="submitForm">生成作品</el-button>
          <el-button @click="dialogVisible = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts" setup>
import { list, delDemo, listarr, works } from '@/api/system/liveStream/index';
import { DemoVO, DemoVOarr, DemoForm, DemoQuery } from '@/api/system/liveStream/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

import mp4Url from '@/assets/video/1.mp4';
import Videojs from 'video.js';
import 'video.js/dist/video-js.min.css';
import { getToken } from '@/utils/auth';
import { ref } from 'vue';
import { ElMessage } from 'element-plus';

const token = getToken();
const Authorization = `Bearer ${token}`;
const clientid = import.meta.env.VITE_APP_CLIENT_ID;
const headers = ref({ 'Authorization': Authorization, 'clientid': clientid });

const initFormData: DemoForm = {
  id: undefined,
  name: undefined,
  type: undefined,
  lights: undefined,
  describe: undefined,
  value: undefined
};

const data = reactive<PageData<DemoVO, DemoForm>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10
  },
  rules: {
    name: [{ required: true, message: '产品名称不能为空', trigger: 'blur' }],
    type: [{ required: true, message: '产品类别不能为空', trigger: 'blur' }],
    lights: [{ required: true, message: '产品亮点不能为空', trigger: 'blur' }],
    describe: [{ required: true, message: '产品说明书不能为空', trigger: 'blur' }]
  }
});

const queryFormRef = ref<ElFormInstance>();
const dataList = ref<DemoVO[]>([]);
const dataListarr = ref<DemoVOarr[]>([]);
const dataFormRef = ref<ElFormInstance>();
const total = ref(0);
const totalarr = ref(0);
const loading = ref(true);
const videoRef = ref(null);
const videoInstance = shallowRef(); //提高性能
let fileList = ref([]);
let fileList1 = ref([]);

// 定义表单数据
let dialogVisible = ref(false);
const buttonLoading = ref(false);
let form = ref({});
const uploadUrl = 'http://mes.jshengzheng.com:8068/prod-api/resource/oss/upload';
let formData = ref({});
const userIds = ref<Array<string | number>>([]);
const userId = ref<Array<string | number>>([]);
const multiple = ref(true);
const dialog = {
  title: ''
};
const { queryParams, rules } = toRefs(data);

let words = ref();
const options = [
  {
    value: '1',
    label: '1'
  },
  {
    value: '2',
    label: '2'
  },
  {
    value: '3',
    label: '3'
  },
  {
    value: '4',
    label: '4'
  }
];

// 多选框选中数据
const handleSelectionChange = (selection: DemoVO[]) => {
  userIds.value = selection.map((item) => item.id);
  multiple.value = !selection.length;
};
// 多选框选中数据
const handleSelectionChangearr = (selection: DemoVOarr[]) => {
  userId.value = selection.map((item) => item.id);
};

/** 查询列表 */
const getList = async () => {
  loading.value = true;
  const res = await list(queryParams.value);
  dataList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 查询列表 */
const getListarr = async () => {
  const res = await listarr(queryParams.value);
  dataListarr.value = res.rows;
  totalarr.value = res.total;
};

/** 搜索按钮操作 */
const handleQuery = () => {
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  words = ref();
  dataFormRef.value?.resetFields();
};

const potions = {
  autoplay: false, //是否自动播放
  loop: false, //是否循环播放
  controls: false, //控制器
  preload: false, //是否预加载
  language: 'zh-CN', //显示的语言 中文
  playbackRates: [0.5, 1, 1.5, 2], //播放速度
  poster: '' //封面图
};

/** 选择数字人按钮操作 */
const handleAdd = () => {
  if (userIds.value.length != 0) {
    getListarr();
    dialogVisible.value = true;
    dialog.title = '选择数字人';
  } else {
    ElMessage({
      showClose: true,
      message: '请选择产品',
      type: 'warning'
    });
  }
};

/** 删除按钮操作 */
const handleDelete = async (row?: DemoVO) => {
  const _ids = row?.id;
  await proxy?.$modal.confirm('是否确认删除名称为"' + row.productName + '"的数据项？').finally(() => (loading.value = false));
  await delDemo(_ids);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};

const submitForm = () => {
  if (userId.value.length != 0) {
    const arr = {
      scriptIds: userIds.value,
      digitalIds: userId.value
    };
    works(arr).finally(() => (buttonLoading.value = false));
    proxy?.$modal.msgSuccess('生成作品成功');
    dialogVisible.value = false;
  } else {
    ElMessage({
      showClose: true,
      message: '请选择数字人',
      type: 'warning'
    });
  }
};

/**
 * 初始化video
 */
const initVideo = () => {
  if (!videoRef.value) return;
  // Videojs第一个参数可以使video标签的id或者目标标签元素，第二个参数为配置项。
  videoInstance.value = Videojs(videoRef.value, potions);
};
onMounted(() => {
  getList();
  initVideo();
});
</script>

<style scoped lang="scss">
.home {
  height: 100%;

  blockquote {
    padding: 10px 20px;
    margin: 0 0 20px;
    font-size: 17.5px;
    border-left: 5px solid #eee;
  }
}
</style>
